package Sequence;

public class Test0695 {
}

class Solution0695_1 {
    public int maxAreaOfIsland(int[][] grid) {
        int length1 = grid.length;
        int length2 = grid[0].length;
        int ans = 0;

        for (int i = 0; i < length1; i++) {
            for (int j = 0; j < length2; j++) {
                if (grid[i][j] == 1) {
                    ans = Math.max(ans, dfs(grid, i, j));
                }
            }
        }

        return ans;
    }

    public int dfs(int[][] grid, int i, int j) {
        if (i<0||j<0||i>=grid.length||j>=grid[0].length||grid[i][j] == 0)
            return 0;

        grid[i][j] = 1;
        int ans = 0;
        ans += dfs(grid, i + 1, j);
        ans += dfs(grid, i - 1, j);
        ans += dfs(grid, i, j + 1);
        ans += dfs(grid, i, j - 1);
        return ans;
    }
}
